<template>
  <div>
    <div style="margin-top: 2%;text-align:center;">
      <el-select  v-model="form.academyId"
                  style="width: 16%;margin-right: 1%"
                  clearable filterable
                  placeholder="请选择学院"
                  @change="professionInit(form.academyId)">
        <el-option v-for="item in academy" :key="item.academyId"
                   :label="item.academyId +' - '+ item.academyName"
                   :value="item.academyId">
        </el-option>
      </el-select>
      <el-select  v-model="form.professionId"
                  style="width: 16%;margin-right: 1%"
                  clearable filterable
                  placeholder="请选择专业">
        <el-option v-for="item in profession" :key="item.professionId"
                   :label="item.professionName" :value="item.professionId">
        </el-option>
      </el-select>
      <el-input style="width: 24%;margin-right: 1%" v-model="form.stuName"
                placeholder="请输入待查找的学号/姓名/身份证/手机号(可指定学院/专业)"></el-input>
      <el-button @click="findStudent()" style="font-size: 20px" type="primary">立即查找</el-button>
      <el-button @click="addStudent()" style="font-size: 20px" type="primary">点击此处新增学生</el-button>
    </div>

    <template style="margin-top: 2%">
      <el-table :data="pageInfo.list" style="width: 100%">
        <el-table-column label="" width="30">
        </el-table-column>
        <el-table-column label="学号" width="120" prop="stuId" sortable>
          <template slot-scope="scope">
            <span>{{scope.row.stuId}}</span>
          </template>
        </el-table-column>
        <el-table-column label="姓名" width="200" prop="stuName" sortable>
          <template slot-scope="scope">
            <span>{{scope.row.stuName}}</span>
          </template>
        </el-table-column>
        <el-table-column label="性别" width="80" prop="stuSex" sortable>
          <template slot-scope="scope">
            <span v-if="scope.row.stuSex===1">男</span>
            <span v-if="scope.row.stuSex===2">女</span>
            <span v-if="scope.row.stuSex===3">不明</span>
          </template>
        </el-table-column>
        <el-table-column label="身份证号"  width="240" prop="stuIdCard" sortable>
          <template slot-scope="scope">
            <span>{{scope.row.stuIdCard}}</span>
          </template>
        </el-table-column>
        <el-table-column label="手机" width="160" prop="stuPhone" sortable>
          <template slot-scope="scope">
            <span>{{scope.row.stuPhone}}</span>
          </template>
        </el-table-column>
        <el-table-column label="班级" width="120" prop="clazzId" sortable>
          <template slot-scope="scope">
            <span>{{scope.row.clazzId}}</span>
          </template>
        </el-table-column>
        <el-table-column label="专业" width="260" prop="professionId" sortable>
          <template slot-scope="scope">
            <span>{{scope.row.professionName}}</span>
          </template>
        </el-table-column>
        <el-table-column label="学院" width="260" prop="academyId" sortable>
          <template slot-scope="scope">
            <span>{{scope.row.academyName}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button  size="mini" type="primary" style="font-size: 16px"
                        @click="handleEdit(scope.row)">编辑</el-button>
            <el-button  size="mini" type="warning" style="font-size: 16px"
                        @click="initPwd(scope.row)">重置密码</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin-bottom: 1%;"></div>
      <el-pagination
          style="text-align: center;"
          background
          layout="total,prev, pager, next"
          :total="pageInfo.total"
          :page-size="pageInfo.pageSize"
          @current-change="handleCurrentChange">
      </el-pagination>
    </template>

    <el-dialog title="新增/编辑学生" :visible.sync="dialogForm" :before-close="handleClose">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="20%">
        <el-form-item  label="请输入学号" prop="stuId">
          <el-input style="width: 40%;"
                    placeholder="请输入目标学号"
                    v-model="ruleForm.stuId"
                    oninput ="value=value.replace(/[^0-9]/g,'')"></el-input>
        </el-form-item>
        <el-form-item  label="请输入学生姓名" prop="stuName">
          <el-input style="width: 60%;" v-model="ruleForm.stuName"
                    placeholder="请输入学生姓名"></el-input>
        </el-form-item>
        <el-form-item label="请选择性别" prop="stuSex">
          <el-select v-model="ruleForm.stuSex" style="width: 20%" placeholder="请选择性别">
            <el-option label="男" :value="1"/>
            <el-option label="女" :value="2"/>
            <el-option label="未知" :value="3"/>
          </el-select>
        </el-form-item>
        <el-form-item  label="请输入身份证号" prop="stuIdCard">
          <el-input style="width: 40%;"
                    placeholder="请输入身份证号"
                    v-model="ruleForm.stuIdCard"
                    oninput ="value=value.replace(/[^0-9Xx]/g,'')"></el-input>
        </el-form-item>
        <el-form-item  label="请输入手机号" prop="stuPhone">
          <el-input style="width: 40%;"
                    placeholder="请输入手机号"
                    v-model="ruleForm.stuPhone"
                    oninput ="value=value.replace(/[^0-9]/g,'')"></el-input>
        </el-form-item>
        <el-form-item  label="请选择学院" prop="academyId">
          <el-select  v-model="ruleForm.academyId"
                      style="width: 40%;"
                      clearable filterable
                      placeholder="请选择学院"
                      @change="professionInit(ruleForm.academyId)">
            <el-option v-for="item in academy" :key="item.academyId"
                       :label="item.academyId +' - '+ item.academyName"
                       :value="item.academyId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item  label="请选择专业" prop="professionId">
          <el-select  v-model="ruleForm.professionId"
                      style="width: 40%;"
                      clearable filterable
                      placeholder="请先选择学院"
                      @change="clazzInit(ruleForm.professionId)">
            <el-option v-for="item in profession" :key="item.professionId"
                       :label="item.professionName" :value="item.professionId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item  label="请选择班级" prop="clazzId">
          <el-select  v-model="ruleForm.clazzId"
                      style="width: 40%;"
                      clearable filterable
                      placeholder="请先选择专业">
            <el-option v-for="item in clazz" :key="item.clazzId"
                       :label="item.clazzId" :value="item.clazzId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">立即提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data(){
    return {
      dialogForm: false,
      form:{
        academyId:'',
        professionId:'',
        stuName:'',
      },
      academy:[],
      profession:[],
      clazz:[],
      currPage: 1,
      pageInfo:{},
      ruleForm:{
        oldStuId:'',
        stuId: '',
        stuName: '',
        stuSex: '',
        stuIdCard: '',
        stuPhone: '',
        academyId: '',
        professionId: '',
        clazzId: '',
      },
      rules:{
        stuId: [
          { required: true, message: '请输入学生学号', trigger: 'blur' },
          { min: 2, max: 9, message: '请输入2 - 9位数字!', trigger: 'blur' }
        ],
        stuName: [
          { required: true, message: '请输入学生姓名', trigger: 'blur' },
          { min: 2, max: 20, message: '请输入2 - 20个字符!', trigger: 'blur' }
        ],
        stuIdCard: [
          { required: true, message: '请输入身份证号', trigger: 'blur' },
          { min: 18, max: 18, message: '请输入18位身份证号!', trigger: 'blur' }
        ],
        stuPhone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { min: 11, max: 11, message: '请输入11位手机号!', trigger: 'blur' }
        ],
        clazzId: [
          { required: true, message: '请选择班级', trigger: 'blur' },
        ],
      }
    }
  },
  created() {
    this.academyInit();
    this.getData();
  },
  methods:{
    submitForm(formName){ // 新增/修改学生
      this.$refs[formName].validate((valid) => {//校验格式
        if (valid) {//格式校验通过
          let path = `http://localhost:8081/student/updateStudent/${this.ruleForm.oldStuId}`;
          this.$ajax.post(path,this.ruleForm).then(res=>{
            if(res.data.status === 0){
              this.$message.success(res.data.message);
              this.dialogForm = false;
            }
            else{
              this.$message.error(res.data.message);
            }
            this.getData();
          })
        }
      });
    },
    handleEdit(row){
      this.ruleForm.oldStuId = row.stuId;
      this.ruleForm.stuId = row.stuId + '';
      this.ruleForm.stuName = row.stuName;
      this.ruleForm.stuSex = row.stuSex;
      this.ruleForm.stuIdCard = row.stuIdCard;
      this.ruleForm.stuPhone = row.stuPhone;
      this.ruleForm.academyId = row.academyId;
      this.professionInit(row.academyId);
      this.ruleForm.professionId = row.professionId;
      this.clazzInit(row.professionId);
      this.ruleForm.clazzId = row.clazzId;
      this.dialogForm = true;
    },
    addStudent(){
      this.ruleForm.oldStuId = -1;
      this.ruleForm.stuId = '';
      this.ruleForm.stuName = '';
      this.ruleForm.stuSex = 3;
      this.ruleForm.stuIdCard = '';
      this.ruleForm.stuPhone = '';
      this.ruleForm.academyId = '';
      this.professionInit('');
      this.ruleForm.professionId = '';
      this.clazzInit('');
      this.ruleForm.clazzId = '';
      this.dialogForm = true;
    },
    initPwd(row){//重置密码
      this.$confirm('确认重置学生“' + row.stuId + ' - '+ row.stuName + '“的登录密码?').then(()=>{
        let path = 'http://localhost:8081/student/initStudentPwd';
        this.$ajax.post(path,row).then(res=>{
          if(res.data.status === 0){
            this.$message.success(res.data.message);
          }
          else{
            this.$message.error('密码重置失败!');
          }
        })
      }).catch(() => {
        this.$message('已取消重置密码!');
      });
    },
    academyInit(){//初始化学院
      let path = 'http://localhost:8081/academy/selectAcademyCanUse';
      this.$ajax.post(path).then(res=>{
        this.academy = res.data.list
      })
    },
    professionInit(val){//根据学院初始化专业
      this.form.professionId = '';
      this.ruleForm.professionId = '';
      if(val !== ''){
        let path = `http://localhost:8081/profession/getProfession/${val}`;
        this.$ajax.post(path).then(res=>{
          this.profession = res.data.list
        })
      }
      else{
        this.profession = null;
      }
    },
    clazzInit(val){//根据专业初始化班级
      this.ruleForm.clazzId = '';
      if(val !== ''){
        let path = `http://localhost:8081/clazz/getClazz/${val}`;
        this.$ajax.post(path).then(res=>{
          this.clazz = res.data.list
        })
      }
      else{
        this.clazz = null;
      }
    },
    handleCurrentChange(val){//翻页
      this.currPage = val;
      this.getData();
    },
    findStudent(){
      this.currPage = 1;
      this.getData();
    },
    getData(){
      let path = `http://localhost:8081/student/getStudents/${this.currPage}`;
      this.$ajax.post(path,this.form).then(res=>{
        this.pageInfo = res.data;
      })
    },
    handleClose(done){
      this.$confirm('确认关闭?').then(()=>{
        done();
        this.professionInit(this.form.academyId);
      })
    }
  }
}
</script>

<style scoped>
span{
  font-size: 18px;
}
</style>